package cse3311.calendarapp.gui;

import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import cse3311.calendarapp.EventData;
import cse3311.calendarapp.R;

public class ScheduleGroupEventActivity extends Activity {
	private boolean[] selectedTimeSlots_m;

	EditText eventNameEditText_m;
	EditText eventLocationEditText_m;
	EditText eventGroupEditText_m;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_schedule_group_event);
		selectedTimeSlots_m = getIntent().getBooleanArrayExtra(
				"selectedTimeSlots");

		eventNameEditText_m = (EditText) findViewById(R.id.groupEventNameEditText);
		eventLocationEditText_m = (EditText) findViewById(R.id.groupEventLocationEditText);
		eventGroupEditText_m = (EditText) findViewById(R.id.groupEventGroupEditText);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.schedule_group_event, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		String result = "";

		switch (item.getItemId()) {
		case R.id.action_send_group_event:
			// send the finalize group event activity some simulation data
			// we'll pretend that 4/6 invited people chose the first time slot
			// that was sent

			// first, find the hour of the first selected time slot
			int hour = 0;

			for (int i = 0; i < 24; i++) {
				if (selectedTimeSlots_m[i]) {
					hour = i;
					break;
				}
			}

			// next get the date and create another calendar including the hour
			// chosen
			GregorianCalendar date = (GregorianCalendar) getIntent()
					.getSerializableExtra("date");
			GregorianCalendar eventDate = new GregorianCalendar(
					date.get(GregorianCalendar.YEAR),
					date.get(GregorianCalendar.MONTH),
					date.get(GregorianCalendar.DAY_OF_MONTH), hour, 0);

			// create an EventData object to hold our event info
			EventData data = new EventData();
			data.alias_m = eventNameEditText_m.getText().toString().trim();

			if (data.alias_m.isEmpty()) {
				break;
			}

			data.location_m = eventLocationEditText_m.getText().toString();
			data.start_m = eventDate;
			String groupName = eventGroupEditText_m.getText().toString();
			data.notes_m = "Group meeting with " + groupName;

			// create the intent, pass it all the info it needs, and start the
			// activity
			Intent finalizeGroupEventIntent = new Intent(this,
					FinalizeGroupEventActivity.class);
			finalizeGroupEventIntent.putExtra("groupName", groupName);
			finalizeGroupEventIntent.putExtra("data", data);
			finalizeGroupEventIntent.putExtra("acceptanceRatio", 4.0 / 6);
			startActivity(finalizeGroupEventIntent);
			finish();
			break;
		default:
			return super.onOptionsItemSelected(item);
		}

		return true;
	}
}
